﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Indigo.Quibbler.Common
{
    /// <summary>
    /// Interaction logic for DownloadControl.xaml
    /// </summary>
    public partial class DownloadControl : UserControl
    {
        Storyboard sb;

        public double Duration
        { get; set; }




        public DownloadControl()
        {
            InitializeComponent();
        }

        private DoubleAnimationUsingKeyFrames _CreateDoubleAnimationUsingKeyFrame(TimeSpan beginTime, object target, string targetProperty)
        {
            DoubleAnimationUsingKeyFrames d1 = new DoubleAnimationUsingKeyFrames();
            d1.BeginTime = beginTime;
            d1.SetValue(Storyboard.TargetProperty, target);
            d1.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(targetProperty));
            return d1;
        }
        private SplineDoubleKeyFrame _CreateSplineDoubleKeyFrame(TimeSpan keyTime, double value)
        {
            SplineDoubleKeyFrame s1 = new SplineDoubleKeyFrame();
            s1.KeyTime = KeyTime.FromTimeSpan(keyTime);
            s1.Value = value;
            return s1;
        }

        public void StartAnimation()
        {
            if (sb == null)
            {
                sb = new Storyboard();
                double width = this.Width;
                DoubleAnimationUsingKeyFrames d1 = _CreateDoubleAnimationUsingKeyFrame(TimeSpan.Zero, imgDownloading, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)");
                d1.KeyFrames.Add(_CreateSplineDoubleKeyFrame(TimeSpan.FromSeconds(0), -50));
                d1.KeyFrames.Add(_CreateSplineDoubleKeyFrame(TimeSpan.FromSeconds(this.Duration), width));
                sb.Children.Add(d1);
                sb.RepeatBehavior = RepeatBehavior.Forever;
            }
            imgDownloading.Visibility = Visibility.Visible;
            sb.Begin();
        }

        public void StopAnimation()
        {
            if (sb != null)
            {
                sb.Stop();
                imgDownloading.Visibility = Visibility.Collapsed;
            }
        }
    }
}
